package vp.remedies.dr.home.adapter;

import java.util.List;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.listviewfeed.data.FeedItem;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeContentAdapter extends BaseAdapter {

	private Activity activity;
	private static LayoutInflater inflater = null;
	private List<FeedItem> feedItems;

	public HomeContentAdapter(Activity a, List<FeedItem> feedItems) {
		activity = a;
		this.feedItems = feedItems;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return feedItems.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.home_content_list_row, null);

		TextView title = (TextView) vi.findViewById(R.id.title); // title
		TextView artist = (TextView) vi.findViewById(R.id.artist); // artist
																	// name
		TextView duration = (TextView) vi.findViewById(R.id.duration); // duration
		ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
		ImageView feedImageView = (ImageView) vi.findViewById(R.id.feedImage1); // image
		TextView statusMsg = (TextView) vi.findViewById(R.id.txtStatusMsg);
		TextView url = (TextView) vi.findViewById(R.id.txtUrl);

		FeedItem item = feedItems.get(position);
		// Setting all values in listview
		title.setText(item.getName());
		artist.setText(item.getDesc());
		duration.setText(item.getSlogan());

		// Chcek for empty status message
		if (!TextUtils.isEmpty(item.getStatus())) {
			statusMsg.setText(item.getStatus());
			statusMsg.setVisibility(View.VISIBLE);
		} else {
			// status is empty, remove from view
			statusMsg.setVisibility(View.GONE);
		}

		url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
				+ item.getUrl() + "</a> "));

		// Making url clickable
		thumb_image.setImageResource(R.drawable.dadimakenuskhe);
		// Feed image
		String feedImageViewName = item.getImge();
		if (feedImageViewName.contains("diabetes.jpg")) {
			feedImageView.setImageResource(R.drawable.diabetes);
		} else if (feedImageViewName.contains("killingsexdrive.jpg")) {
			feedImageView.setImageResource(R.drawable.killingsexdrive);
		} else if (feedImageViewName.contains("cancer.jpg")) {
			feedImageView.setImageResource(R.drawable.cancer);
		} else if (feedImageViewName.contains("immune.jpg")) {
			feedImageView.setImageResource(R.drawable.immune);
		} else if (feedImageViewName.contains("heart.jpg")) {
			feedImageView.setImageResource(R.drawable.heart);
		} else if (feedImageViewName.contains("gallstones.jpg")) {
			feedImageView.setImageResource(R.drawable.gallstones);
		}

		return vi;
	}
}